<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv=X-UA-Compatible content="IE=edge"/>
    <meta name=viewport content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>教师信息列表管理</title>
    <link rel="stylesheet" href="../css/element/index.css"/>
    <link rel="stylesheet" href="../css/main.css"/>
    <link rel="stylesheet" href="../css/header.css"/>
    <link rel="stylesheet" href="../css/sidebar.css"/>
    <link rel="stylesheet" href="../css/right.css"/>
</head>
<body>
<div id="app">
    <!--表格-->
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-s-grid"></i> 教师信息表格</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <!-- 表主体开始 -->
        <div class="container">
            <!-- 搜索开始 -->
            <div class="handle-box">
                <el-form :inline="true">
                    <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAll"
                               size="small">批量删除
                    </el-button>
                    <el-select v-model="formMap.sex" placeholder="选择性别" class="handle-select mr10">
                        <el-option key="1" label="男" value="男"></el-option>
                        <el-option key="2" label="女" value="女"></el-option>
                    </el-select>
                    <el-input v-model="formMap.name" placeholder="姓名" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="fetchData" size="small">搜索</el-button>
                    <el-button type="success" icon="el-icon-plus" size="small" @click="showEditWindow">添加教师</el-button>
                    <el-button type="primary" icon="el-icon-download" size="small" @click="outTeacher">导出表信息到桌面
                    </el-button>
                </el-form>
            </div>
            <!-- 搜索结束 -->

            <!-- 弹窗开始 -->
            <el-dialog title="编辑教师信息" :visible.sync="dialogVisible">
                <el-form :model="teacher">
                    <el-form-item label="教师号" :label-width="formWidth">
                        <el-input v-model="teacher.tno" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="姓名" :label-width="formWidth">
                        <el-input v-model="teacher.tname" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" :label-width="formWidth">
                        <el-input v-model="teacher.tpwd" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" :label-width="formWidth">
                        <el-select v-model="teacher.tsex" placeholder="选择性别" class="handle-select mr10">
                            <el-option key="1" label="男" value="男"></el-option>
                            <el-option key="2" label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="年龄" :label-width="formWidth">
                        <el-input v-model="teacher.tage" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="入职时间" :label-width="formWidth">
                        <el-input v-model="teacher.entryYear" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                    <el-button type="primary" @click="save" size="small">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 弹窗结束 -->

            <!-- 表格数据开始 -->
            <el-table ref="multipleTable"
                      :data="tableData" class="table"
                      tooltip-effect="dark" style="width: 100%"
                      @selection-change="handleSelectionChange">
                <!-- 多选框所在的列 -->
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="tno"
                        label="教师号"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="tname"
                        label="姓名"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="tsex"
                        label="性别"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="tage"
                        label="年龄"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="entryYear"
                        label="入职时间"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="tpwd"
                        label="登录密码"
                        width="150">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="200">
                    <template slot-scope="scope">
                        <el-button size="mini" type="info" @click="edit(scope.row.id)">修改
                        </el-button>
                        <el-button size="mini" type="danger" @click="del(scope.row.tno)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 表格数据结束 -->

            <!-- 分页开始 -->
            <div class="pagination">
                <el-pagination
                        @size-change="fetchData"
                        @current-change="fetchData"
                        :current-page.sync="currentPage"
                        :page-sizes="pageSizes"
                        :page-size.sync="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
            <!-- 分页结束 -->
        </div>
        <!-- 表主体结束 -->
    </div>
    <!-- 表格结束 -->
</div>

<script src="../js/vue.min.js"></script>
<script src="../js/element.js"></script>
<script src="../js/axios.min.js"></script>
<script src="../js/basic.js"></script>
<script>
    let bus = new Vue({
        el: '#app',
        data: {
            tableData: [],
            multipleSelection: [],
            dialogVisible: false,
            teacher: {
                id: ''//教师编号
            },
            formWidth: '120px',
            currentPage: 1, // 当前页码
            pageSize: 5, // 每页显示记录数
            pageSizes: [2, 3, 5, 10, 15, 20], // 选择每页显示数
            total: 0, // 总记录数
            formMap: {}, // 用于搜索的对象
        },
        created: function () {
            // 异步获取教师数据
            this.fetchData();
        },
        computed: {//计算
        },
        methods: {
            // 点击添加显示编辑窗口
            showEditWindow() {
                this.teacher = {};
                this.dialogVisible = true;
            },
            //导出教师表
            outTeacher() {
                _this = this;
                this.$confirm('你确定要导出教师表？', '导出确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get(`${httpUrl}/teacher/out`)
                        .then(res => {
                            this.$message({
                                type: 'success',
                                message: res.data.msg
                            });
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消导出'
                    });
                });
            },
            // 获取数据
            fetchData() {
                _this = this;
                axios.post(`${httpUrl}/teacher/list?page=${this.currentPage}&size=${this.pageSize}`, this.formMap)
                    .then(res => {
                        _this.tableData = res.data.row;
                        _this.total = res.data.total;
                    }).catch(err => {
                    this.$message({
                        showClose: true,
                        message: err.message,
                        type: 'error'
                    });
                });
            },
            // 保存或者修改教师
            save() {
                _this = this;
                axios.post(`${httpUrl}/teacher/${this.teacher.id == null ? 'add' : 'update'}`, this.teacher)
                    .then(res => {
                        let r = res.data;
                        if (r.code !== 200) {
                            this.$message({
                                showClose: true,
                                message: r.msg,
                                type: 'error'
                            });
                        } else {
                            this.$message({
                                showClose: true,
                                message: r.msg,
                                type: 'success'
                            });
                            // 关闭弹窗
                            _this.dialogVisible = false;
                            _this.teacher = {}
                            _this.teacher.id = r.data
                            // 重新获取一次数据
                            _this.fetchData();
                        }
                    });
            },
            // 根据id查询
            edit(id) {
                _this = this;
                axios.get(httpUrl + '/teacher/getById?id=' + id)
                    .then(res => {
                        // 显示编辑窗口
                        _this.dialogVisible = true
                        // 给编辑窗口设置值
                        _this.teacher = res.data.data
                        // 重新获取一次数据
                        _this.fetchData();
                    }).catch(er => {
                    this.$message({
                        showClose: true,
                        message: er.message,
                        type: 'error'
                    });
                })
            },
            // 删除
            del(tNo) {
                _this = this;
                this.$confirm('你确定要删除么？', '删除确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get(`${httpUrl}/teacher/delete?tNo=` + tNo)
                        .then(res => {
                            this.$message({
                                type: 'success',
                                message: res.data.msg
                            });
                            _this.fetchData();
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 批量删除
            delAll() {
                const length = this.multipleSelection.length;
                if (length != 0) {
                    let str = '';
                    for (let i = 0; i < length; i++) {
                        if (i < length - 1) {
                            str += this.multipleSelection[i].id + ',';
                        } else {
                            str += this.multipleSelection[i].id;
                        }
                    }
                    axios.get(`${httpUrl}/teacher/batchDel?ids=${str}`)
                        .then(res => {
                            this.$message.success(res.data.msg);
                            this.multipleSelection = [];
                            // 重新获取一次数据
                            _this.fetchData();
                        })
                } else {
                    this.$message.error('请选择要删除的教师！');
                }
            },
            // 搜索
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    })
</script>
</body>
</html>